
const {getOrderGoods,createOrder,addOrdergoods,getorders,removeOrder,updateOrder, getcommoditylist,Addpay,getAddpay,removepay,createLogistics,getLogistics} = require('../service/order.service');
class OrderController {
//创建订单
    async addOrder(ctx, next) {
        //解析接口数据
        //操作数据库
        try {
            const {order_admin,order_name, order_id, order_price, order_status,order_phone,order_address,order_province,order_remark,order_goodslist} = ctx.request.body;
            const res= await createOrder({order_admin,order_name, order_id, order_price, order_status,order_phone,order_address,order_province,order_remark})
         //返回数据
            
            const result=await addOrdergoods(order_goodslist,order_id)    
            ctx.body = {res,result};
            
        } catch (error) {
            
        }
    }
    //获取订单列表
    async getOrders(ctx, next) {
        //解析接口数据 
        try {
        const { order_id, order_name, order_status,order_phone,order_province} = ctx.request.query;
        console.log(ctx.request.body);
        console.log( order_id,order_name, order_status,order_phone,order_province);

        //操作数据库
       
            const res = await getorders({order_name, order_id, order_status,order_phone,order_province});
            //返回数据
            ctx.body = {res};
        } catch (error) {
            console.log(error);
        }       

    }
    //删除订单
    async deleteOrder(ctx) {
        //解析接口数据
        //操作数据库
        try {
            const  num= await getOrderGoods(ctx.params.id)
            const res = await removeOrder(ctx.params.id)
            //返回数据
            ctx.body = {res};
        } catch (error) {
            console.log(error);
        }   
            
    }
    async updateOrder(ctx) {
        //解析接口数据
        //操作数据库
        try {
            const  order_id  = ctx.params.id
           
            const {  order_name, order_status,order_phone,order_province,order_address,order_remark} = ctx.request.body;
            //console.log(order_id,order_name, order_status,order_phone,order_province,order_address,order_remark);
            const res = await updateOrder({order_id, order_name, order_status,order_phone,order_province,order_address,order_remark})
            //返回数据
            ctx.body = {res};
        } catch (error) {
            console.log(error);
        }   
    } 
    async getcommodity(ctx) {
        //解析接口数据
        //操作数据库
        try {
            
            const res = await getcommoditylist();
            //返回数据
            console.log(res);
            ctx.body = {res};
            
        } catch (error) {
            console.log(error);
        }   
    }
    async addpay(ctx) {
        //解析接口数据
        //操作数据库
        try {
            const {order_id,pay_url,pay_id,pay_number} = ctx.request.body;
            console.log(order_id,pay_url,pay_id,pay_number);
            const res = await Addpay({order_id,pay_url,pay_id,pay_number});
            //返回数据
            ctx.body = {res};
        } catch (error) {
            console.log(error);
        }
    }
    
    async getpay(ctx) {
        
        try {
            //操作数据库
           
                const res = await getAddpay(ctx.params.id);
                //返回数据
                ctx.body = {res};
            } catch (error) {
                console.log(error);
            }}
    async delpay(ctx) {
        //解析接口数据
        //操作数据库
        try {
            const res = await removepay(ctx.params.id)
            //返回数据
            ctx.body = {res};
        } catch (error) {
            console.log(error);
        }   
    }
    async logistics(ctx) {
        //解析接口数据
        //操作数据库
        try {
            const {order_id,logistics_name,logistics_number,logistics_time} = ctx.request.body;
            console.log(order_id,logistics_name,logistics_number,logistics_time);
            const res = await createLogistics({order_id,logistics_name,logistics_number,logistics_time});
            //返回数据
            ctx.body = {res};
        } catch (error) {
            console.log(error);
        }
        }
        async getlogistics(ctx) {
        
            try {
                //操作数据库
               
                    const res = await getLogistics(ctx.params.id);
                    //返回数据
                    ctx.body = {res};
                } catch (error) {
                    console.log(error);
                }}

}

module.exports = new OrderController();